//
//  CZComposeBottomView.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/5.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

// 按钮枚举
enum CZComposeBottomViewType:Int {
    case picture = 1001
    case mention = 1002
    case trend = 1003
    case emoticon = 1004
    case add = 1005
}

class CZComposeBottomView: UIView {

    // 声明一个闭包
    var closure:((CZComposeBottomViewType)->())?
    
    // 第四个按钮
    var emoticonButton: UIButton?
    
    // 判断是否是表情键盘
    var isEmoticon: Bool = false{
        didSet{
            // 默认图片是笑脸 -> 系统键盘 isEmoticon = false
            // 如果图片是键盘 -> 表情键盘 isEmoticon = true
            // 系统键盘
            // 图片是笑脸
            var imgName = "compose_emoticonbutton_background"
            // 表情键盘
            if  isEmoticon {
                // 图片是键盘
                imgName = "compose_keyboardbutton_background"
            }
            // 第四个按钮设置image
            emoticonButton?.setImage(UIImage(named:imgName), for: UIControlState.normal)
            emoticonButton?.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        let pictureButton = addChildButtons(imgName: "compose_toolbar_picture",type: .picture)
        let mentionButton = addChildButtons(imgName: "compose_mentionbutton_background",type: .mention)
        let trendButton = addChildButtons(imgName: "compose_trendbutton_background",type: .trend)
        emoticonButton = addChildButtons(imgName: "compose_emoticonbutton_background",type: .emoticon)
        let addButton = addChildButtons(imgName: "compose_add_background",type: .add)
        
        pictureButton.snp.makeConstraints { (make) in
            make.top.bottom.left.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureButton.snp.right)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionButton.snp.right)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendButton.snp.right)
            make.width.equalTo(addButton)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(emoticonButton!.snp.right)
            make.right.equalTo(self)
        }
    }
    
    // MARK: - 创建按钮
    private func addChildButtons(imgName: String, type: CZComposeBottomViewType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named: imgName), for: UIControlState.normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), for: UIControlState.highlighted)
        addSubview(button)
        return button
    }
}

extension CZComposeBottomView{
    @objc fileprivate func buttonClick(button: UIButton){
        // 执行闭包
        closure?(CZComposeBottomViewType(rawValue: button.tag)!)
    }
}
